//
//  IMMapMessageTableViewCell.m
//  markmap
//
//  Created by 甘世清 on 2024/5/28.
//

#import "IMMapMessageTableViewCell.h"
#import "LookMapViewController.h"
@implementation IMMapMessageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self.bgView = [[UIView alloc]init];
    self.bgView.layer.cornerRadius = 10;
    self.bgView.backgroundColor = SQHexColor(@"#FFFFFF", 0.4);
    self.bgView.layer.borderColor = SQHexColor(@"#FFFFFF", 0.4).CGColor;
    self.bgView.layer.borderWidth = 0.4;
    self.bgView.layer.masksToBounds = YES;
    self.bgView.clipsToBounds = YES;
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.avatarImageView);
            make.width.mas_equalTo(200);
            make.height.mas_equalTo(100);
    }];
    [self.bgView addSubview:self.addressLabel];
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView);
        make.left.equalTo(self.bgView).offset(6);
        make.right.equalTo(self.bgView.mas_right).offset(-6);
        make.height.mas_equalTo(30);
    }];
    [self.bgView addSubview:self.mapImageView];
    [self.mapImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.bgView);
        make.top.equalTo(self.addressLabel.mas_bottom);
    }];
    
    [self.contentView addSubview:self.blackImageView];
    [self.blackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(24);
        make.right.equalTo(self.bgView.mas_left).offset(-4);
        make.bottom.equalTo(self.bgView.mas_bottom);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SQ_mapViewClick)];
    [self.bgView addGestureRecognizer:tap];
}
- (void)setModel:(IMMessageModel *)model{
    [super setModel:model];
    self.addressLabel.text = model.content;
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (model.sendType) {
            make.left.equalTo(self.contentView).offset(12+48+10);
        } else {
            make.left.equalTo(self.contentView).offset(SCREEN_WIDETH-12-48-10-200);
        }
    }];
    if ([model.ext isNotEmpty]) {
        NSDictionary * ext = [[NSDictionary alloc]initWithDictionary:[model.ext mj_JSONObject]];
        [self.mapImageView sd_setImageWithURL:SQURLImage([ext objectForKey:@"url"])];
    }
}
-(void)SQ_mapViewClick{
    NSDictionary * ext = [[NSDictionary alloc]initWithDictionary:[self.model.ext mj_JSONObject]];
    PointModel * mo = [PointModel new];
    mo.lat = [[NSString stringWithFormat:@"%@",[ext objectForKey:@"lat"]] floatValue];
    mo.lng = [[NSString stringWithFormat:@"%@",[ext objectForKey:@"lng"]] floatValue];
    mo.address = self.model.content;
    LookMapViewController * vc = [[LookMapViewController alloc]init];
    vc.model = mo;
    vc.isIM = YES;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}

- (UILabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc]init];
        _addressLabel.textColor = UIColor.whiteColor;
        _addressLabel.font = FONT_REGULAR(12);
        _addressLabel.userInteractionEnabled = NO;
    }
    return _addressLabel;
}
- (UIImageView *)mapImageView{
    if (!_mapImageView) {
        _mapImageView = [[UIImageView alloc]init];
        _mapImageView.contentMode = UIViewContentModeScaleAspectFill;
        _mapImageView.clipsToBounds = YES;
        _mapImageView.userInteractionEnabled = NO;
    }
    return _mapImageView;
}
@end
